package org.tlipoca.smsteamstore.listener;

import com.rabbitmq.client.Channel;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.genntii.smdomain.entity.SteamStore;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import org.tlipoca.smsteamstore.service.EsService;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Component
@RequiredArgsConstructor
@Slf4j
public class SteamStoreEsListener {

    @Resource
    private EsService esService;

    @RabbitListener(bindings = @QueueBinding(
                    value = @Queue(name = "steamStore.queue", durable = "true"),
                    exchange = @Exchange(name = "steamStore.topic", type = "topic"),
                    key = "steamStore.routingKey"
            ))
    public void receiver(Map map, Channel channel, Message message)throws IOException{

        String type = String.valueOf(((HashMap.Entry) (map).entrySet().toArray()[12]).getValue());

        log.info("监听到数据修改，类型为："+type);

        switch (type){
            case "INSERT":
                esService.insertSteamStore(getSteamStoreByObjectArrayMap(map));
                break;
            case "DELETE":
                esService.deleteSteamStore(getSteamStoreByObjectArrayMap(map));
                break;
            case "UPDATE":
                esService.deleteSteamStore(getSteamStoreByObjectArrayMap(map));
                esService.insertSteamStore(getSteamStoreByObjectArrayMap(map));
                break;
            case "SELECT", "CREATE", "ERASE":
            default:
                break;
        }

      channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

    public static SteamStore getSteamStoreByObjectArrayMap(Map map){
        Object[] array = ((HashMap) ((ArrayList) ((HashMap.Entry) (map).entrySet().toArray()[0]).getValue()).get(0)).entrySet().toArray();
        SteamStore steamStore = SteamStore.builder()
                .id(Long.valueOf(String.valueOf(((Map.Entry)array[0]).getValue())))
                .appid(Integer.valueOf(String.valueOf(((Map.Entry)array[1]).getValue())))
                .name(String.valueOf(((Map.Entry)array[2]).getValue()))
                .headerImage(String.valueOf(((Map.Entry)array[3]).getValue()))
                .capsuleImage(String.valueOf(((Map.Entry)array[4]).getValue()))
                .detailDescription(String.valueOf(((Map.Entry)array[5]).getValue()))
                .shortDescription(String.valueOf(((Map.Entry)array[6]).getValue()))
                .isFree(Boolean.parseBoolean(String.valueOf(((Map.Entry)array[7]).getValue())))
                .requiredAge(String.valueOf(((Map.Entry)array[8]).getValue()))
                .isDlc(Boolean.parseBoolean(String.valueOf(((Map.Entry)array[9]).getValue())))
                .build();
        if (((Map.Entry)array[10]).getValue()!=null){
            steamStore.setFullGameAppid(Integer.valueOf(String.valueOf(((Map.Entry)array[10]).getValue())));
        }
        return steamStore;
    }
}
